//index.js

var color = require('../../lib/color.js');
var itemColor = color.itemColor;
var util = require('../../utils/util.js');
var todoListType = require('../../lib/todoList.js');
var collectListType = require('../../lib/collectList.js');

var wxtouch = require('../../lib/wxtouch.js');
//获取应用实例
var app = getApp();
var system = app.system;
var store = app.store;

var sortColItem = {
  init: function(page, itemHeight) {
    this.x = 0;
    this.y = 0;
    this.scaleRatio = 1;
    this.page = page;
    var renderData = this.page.getRenderData();
    renderData.sortColTextWidth = app.COLLECT_TEXT_WIDTH;
    renderData.sortColCountWidth = app.ITEM_HEIGHT;

  },
  show: function() {
    var renderData = this.page.getRenderData();
    renderData.sortColItemDisplay = 'block';
  },
  hide: function() {
    var renderData = this.page.getRenderData();
    renderData.sortColItemDisplay = 'none';
  },
  setStyle: function(height, text, color, bgcolor, count) {
    var renderData = this.page.getRenderData();
    renderData.sortColItemHeight = height;
    renderData.sortColItemText = text;
    renderData.sortColItemTextColor = color;
    renderData.sortColItemBgColor = bgcolor;
    renderData.sortColCount = count;
  },
  move: function(x, y) {
    this.x = x;
    this.y = y;
    var renderData = this.page.getRenderData();
    var animation = wx.createAnimation({duration:0});
    animation.translate3d(this.x, this.y, 0).scale(this.scaleRatio).step();
    renderData.sortColItemAnimation = animation.export();
  },
  sortstart: function() {
    this.scaleRatio = 1.1;
    var renderData = this.page.getRenderData();
    var animation = wx.createAnimation({duration:250, timingFunction:'linear'});
    animation.translate3d(this.x, this.y, 0).scale(this.scaleRatio).step();
    renderData.sortColItemAnimation = animation.export();
  },
  sortend: function(x, y, callback) {
    this.scaleRatio = 1;
    this.x = x;
    this.y = y;
    var renderData = this.page.getRenderData();
    var animation = wx.createAnimation({duration:300, timingFunction:'linear'});
    animation.translate3d(this.x, this.y, 0).scale(this.scaleRatio).step();
    renderData.sortColItemAnimation = animation.export();
    if (typeof callback === 'function') {
      setTimeout(callback, 300);
    }
  }
}; 

var sortItem = {
  init: function(page) {
    this.x = 0;
    this.y = 0;
    this.scaleRatio = 1;
    this.page = page;
  },
  show: function() {
    var renderData = this.page.getRenderData();
    renderData.sortItemDisplay = 'block';
  },
  hide: function() {
    var renderData = this.page.getRenderData();
    renderData.sortItemDisplay = 'none';
  },
  setStyle: function(height, text, color, bgcolor, textDecoration) {
    var renderData = this.page.getRenderData();
    renderData.sortItemHeight = height;
    renderData.sortItemText = text;
    renderData.sortItemTextColor = color;
    renderData.sortItemBgColor = bgcolor;
    renderData.sortItemTextDecoration = textDecoration;
  },
  move: function(x, y) {
    this.x = x;
    this.y = y;
    var renderData = this.page.getRenderData();
    var animation = wx.createAnimation({duration:0});
    animation.translate3d(this.x, this.y, 0).scale(this.scaleRatio).step();
    renderData.sortItemAnimation = animation.export();
  },
  sortstart: function() {
    this.scaleRatio = 1.1;
    var renderData = this.page.getRenderData();
    var animation = wx.createAnimation({duration:250, timingFunction:'linear'});
    animation.translate3d(this.x, this.y, 0).scale(this.scaleRatio).step();
    renderData.sortItemAnimation = animation.export();
  },
  sortend: function(x, y, callback) {
    this.scaleRatio = 1;
    this.x = x;
    this.y = y;
    var renderData = this.page.getRenderData();
    var animation = wx.createAnimation({duration:300, timingFunction:'linear'});
    animation.translate3d(this.x, this.y, 0).scale(this.scaleRatio).step();
    renderData.sortItemAnimation = animation.export();
    if (typeof callback === 'function') {
      setTimeout(callback, 300);
    }
  } 
};

var collectTextarea = {
  init:function(page) {
    this.page = page;
  },
  setText: function(text) {
    var renderData = this.page.getRenderData();
    renderData.collectTextareaValue = text;
  },
  show: function() {
    var renderData = this.page.getRenderData();
    renderData.collectTextareaDisplay = true;
  },
  hide: function() {
    var renderData = this.page.getRenderData();
    renderData.collectTextareaDisplay = false;
  },
  setFocus: function(focus) {
    this.focus = focus;
    var renderData = this.page.getRenderData();
    renderData.collectTextareaFocus = focus;
  },
  setWidth: function(width) {
    this.width = width;
    var renderData = this.page.getRenderData();
    renderData.collectTextareaWidth = width;
  }
};

var textarea = {
  init:function(page) {
    this.page = page;
  },
  setText: function(text) {
    var renderData = this.page.getRenderData();
    renderData.textareaValue = text;
  },
  show: function() {
    var renderData = this.page.getRenderData();
    renderData.textareaDisplay = true;
  },
  hide: function() {
    var renderData = this.page.getRenderData();
    renderData.textareaDisplay = false;
  },
  setFocus: function(focus) {
    this.focus = focus;
    var renderData = this.page.getRenderData();
    renderData.textareaFocus = focus;
  }
};


Page({
  data: {
    itemList:[],
    collectList:[],
    log:'log',
  },
  render: function() {
    this.setData(this.renderData);
    this.renderData = {};
  },
  getRenderData: function() {
    return this.renderData;
  },
  ontouchstart: function(e, input) {
    if (this.isEditing) {
      return;
    }
    console.log(e.touches[0].clientX);
    if (e.touches[0].clientX < 40) {
      this.isQutiTouch = true;
      return;
    }
    if (e.target.id === 'id-container') {
      this.currentList.touchstart(e, input);
    }
    else if (e.target.id === 'id-item-list') {
      this.todoList.touchstart(e, input);
    }
    else if (e.target.id === 'id-collect-list') {
      this.collectList.touchstart(e, input);
    }
    else if (e.target.id.indexOf('id-item') === 0) {
       this.todoList.touchstart(e, input);
    }
    else if (e.target.id.indexOf('id-col') === 0) {
      this.collectList.touchstart(e, input);
    }
  },
  ontouchmove: function(e, input) {
    if (this.isEditing) {
      return;
    }
    if (this.isQutiTouch) {
      return;
    }
    if (e.target.id === 'id-container') {
      this.currentList.touchmove(e, input);
    }
    else if (e.target.id === 'id-item-list') {
      this.todoList.touchmove(e, input);
    }
    else if (e.target.id === 'id-collect-list') {
      this.collectList.touchmove(e, input);
    }
    else if (e.target.id.indexOf('id-item') === 0) {
      this.todoList.touchmove(e, input);
    }
    else if (e.target.id.indexOf('id-col') === 0) {
      this.collectList.touchmove(e, input);
    }
  },
  ontouchend: function(e, input) {
    if (this.isEditing) {
      return;
    }
    if (this.isQutiTouch) {
      this.isQutiTouch = false;
      return;
    }
    if (e.target.id === 'id-container') {
      this.currentList.touchend(e, input);
    }
    else if (e.target.id === 'id-item-list') {
      this.todoList.touchend(e, input);
    }
    else if (e.target.id === 'id-collect-list') {
       this.collectList.touchend(e, input);
    }
    else if (e.target.id.indexOf('id-item') === 0) {
      this.todoList.touchend(e, input);
    }
    else if (e.target.id.indexOf('id-col') === 0) {
      this.collectList.touchend(e, input);
    }
    console.log(e);
    console.log(input);
  },
  ontouchcancel: function(e, input) {
    if (this.isEditing) {
      return;
    }
    this.ontouchend(e, input);
  },
  ontap: function(e, input) {
    if (this.isEditing) {
      return;
    }
    if (e.target.id === 'id-container') {
      this.currentList.ontap(e, input);
    }
    else if (e.target.id === 'id-item-list') {
      this.todoList.ontap(e, input);
    }
    else if (e.target.id === 'id-collect-list') {
      this.collectList.ontap(e, input);
    }
    else if (e.target.id.indexOf('id-item') === 0) {
      this.todoList.ontap(e, input);
    }
    else if (e.target.id.indexOf('id-col') === 0) {
      this.collectList.ontap(e, input);
    }
  },
  onlongtap: function(e, input) {
    if (this.isEditing) {
      return;
    }
    else if (e.target.id.indexOf('id-item') === 0) {
      this.todoList.onlongtap(e, input);
    }
    else if (e.target.id.indexOf('id-col') === 0) {
      this.collectList.onlongtap(e, input);
    }
  },
  initTouchHandler: function() {
    var s = this;
    wxtouch.init(this);
    this.touchHandler = wxtouch.createHandler
    ('id-container', 'touch', function(e, input) {
      if (wxtouch.istouchstart(input)) {
        s.ontouchstart(e, input);
      }
      else if (wxtouch.istouchmove(input)) {
        s.ontouchmove(e, input);
      }
      else if (wxtouch.istouchend(input)) {
        s.ontouchend(e, input);
      }
      else if (wxtouch.istouchcancel(input)) {
        s.ontouchcancel(e, input);
      }
    });
    this.touchHandler.on('tap', function(e, input) {
      s.ontap(e, input);
    });
    this.touchHandler.on('longtap', function(e, input) {
      s.onlongtap(e, input);
    })

  },
  initTextarea: function() {
    textarea.init(this);
    textarea.setFocus(false);
    textarea.hide();
    collectTextarea.init(this);
    collectTextarea.setFocus(false);
    collectTextarea.hide();
    collectTextarea.setWidth(system.windowWidth - app.ITEM_HEIGHT);
    this.render();
  },
  initSortItem: function() {
    sortItem.init(this);
    sortItem.hide();
    sortColItem.init(this);
    sortColItem.hide();
    this.render();
  },
  onReady: function() {
  },
  onLoad: function () {
    wx.setNavigationBarTitle({title:"clear"});
    
    this.renderData = {};
    this.renderData.editItemTop = -app.checkBound;
    this.renderData.topSwitchTop = -app.checkBound;

    this.initTextarea();
    this.initSortItem();

    store.init(this, this.onGetData, this.onSetData);
    this.collectList = collectListType.createCollectList();
    this.todoList = todoListType.createTodoList();

    this.collectList.initCollectList(this, system.windowWidth, system.windowHeight, 'collectList', this.todoList);
    this.collectList.setInput(collectTextarea);
    this.collectList.setSortItem(sortColItem);

    this.todoList.initTodoList(this, system.windowWidth, system.windowHeight, 'itemList', this.collectList);
    this.todoList.setInput(textarea);
    this.todoList.setSortItem(sortItem);

    if (app.isFirst) {
      this.firstLauncher();
    }
    else {
      var colDataList = store.getCollectFromStoreSync();
      this.collectList.load(colDataList)
      this.collectList.setLastPos(0);
      var setup = app.getSetup();
      this.collectList.currentCollectId = setup.lastShowCollectId;
      if (this.collectList.currentCollectId !== '') {
          var collectItem = this.collectList.getCurrentCollectItem();
          var dataList = store.getItemListFromStoreSync(this.collectList.currentCollectId);
          this.todoList.load(collectItem, dataList);
      }
      if (setup.lastShowType === util.SHOW_TYPE_ITEM) {
        wx.setNavigationBarTitle({title:this.todoList.getTitle()});
        this.collectList.positionForPullDown();
        this.currentList = this.todoList;
      }
      else {
        this.todoList.positionForPullUp();
        this.currentList = this.collectList;
      }
    }
    this.initTouchHandler();
  },
  onGetData: function(e) {

  },
  onSetData: function(e) {

  },
  firstLauncher: function(e) {
     var data = store.buildDefaultData();
     store.saveCollectList(data.collectList);
     store.saveItemList(data.collectId, data.itemList);

     this.collectList.currentCollectId = data.collectId;;
     this.collectList.load(data.collectList);
     this.collectList.setLastPos(0);
     
     var collectItem = this.collectList.getCurrentCollectItem();
     this.collectList.positionForPullDown();
     this.todoList.load(collectItem, data.itemList);
     wx.setNavigationBarTitle({title:this.todoList.getTitle()});
     this.currentList = this.todoList;

     app.setSetup(data.collectId, util.SHOW_TYPE_ITEM);
  },
  onlinechange: function(e) {
    console.log(e);
    this.currentList.onlinechange(e);
  },
  onblur: function(e) {
    this.isEditing = false;
    this.currentList.onEditDone(e.detail.value);
  },
})
